use core::behavior_tree::{BehaviorTree, NodeTypeRegister};
use core::base_node::{TBaseNode, NodeStatus};
use core::tick::Tick;
use core::blackboard::Blackboard;
use actions::error::Error;
use actions::failer::Failer;
use actions::runner::Runner;
use actions::succeeder::Succeeder;
use actions::wait::Wait;
use composites::mem_priority::MemPriority;
use composites::mem_sequence::MemSequence;
use composites::priority::Priority;
use composites::sequence::Sequence;
use decorators::inverter::Inverter;
use decorators::limiter::Limiter;
use decorators::max_time::MaxTime;
use decorators::repeat_until_failure::RepeatUntilFailure;
use decorators::repeat_until_success::RepeatUntilSuccess;
use decorators::repeater::Repeater;

pub struct B3 {
    behavior_tree: BehaviorTree,
    node_type_register: NodeTypeRegister,
    root: Box<dyn TBaseNode>,
    blackboard: Blackboard
}

impl B3 {
    pub fn new_empty(data: serde_json::Value) -> Result<B3, String> {
        let mut register: NodeTypeRegister = NodeTypeRegister::new();
        let bt = match BehaviorTree::load(data, &mut register) {
            Ok(_bt) => { _bt }
            Err(_e) => { return Err(_e) }
        };

        let b3 = B3 {
            behavior_tree: bt.0,
            root: bt.1,
            node_type_register: register,
            blackboard: Blackboard::new()
        };
        Ok(b3)

    }

    pub fn new_default(data: serde_json::Value) -> Result<B3, String> {
        let mut register: NodeTypeRegister = NodeTypeRegister::new();
        Error::register(&mut register, "Error".to_string());
        Failer::register(&mut register, "Failer".to_string());
        Runner::register(&mut register, "Runner".to_string());
        Succeeder::register(&mut register, "Succeeder".to_string());
        Wait::register(&mut register, "Wait".to_string());
        MemPriority::register(&mut register, "MemPriority".to_string());
        MemSequence::register(&mut register, "MemSequence".to_string());
        Priority::register(&mut register, "Priority".to_string());
        Sequence::register(&mut register, "Sequence".to_string());
        Inverter::register(&mut register, "Inverter".to_string());
        Limiter::register(&mut register, "Limiter".to_string());
        MaxTime::register(&mut register, "MaxTime".to_string());
        RepeatUntilFailure::register(&mut register, "RepeatUntilFailure".to_string());
        RepeatUntilSuccess::register(&mut register, "RepeatUntilSuccess".to_string());
        Repeater::register(&mut register, "Repeater".to_string());
        let bt = match BehaviorTree::load(data, &mut register) {
            Ok(_bt) => { _bt }
            Err(_e) => { return Err(_e) }
        };

        let mut b3 = B3 {
            behavior_tree: bt.0,
            root: bt.1,
            node_type_register: register,
            blackboard: Blackboard::new()
        };
        Ok(b3)
    }

    pub fn tick(b3:&mut B3) -> NodeStatus  {
        let mut tick = Tick::new(&mut (b3.behavior_tree), &mut (b3.blackboard));
        b3.root.execute(&mut tick)
        // b3
    }
}
#[cfg(test)]
mod tests {
    use super::*;
    use std::time::Duration;

    /// test output: sequence->succeeder->inverter->failer->error
    #[test]
    fn test_sequence() {
        let config: serde_json::Value = match serde_json::from_str("{\"version\":\"0.3.0\",\"scope\":\"project\",\"selectedTree\":\"5d0cc080-a0d4-4a1b-8898-0530d28dce03\",\"trees\":[{\"version\":\"0.3.0\",\"scope\":\"tree\",\"id\":\"5d0cc080-a0d4-4a1b-8898-0530d28dce03\",\"title\":\"TestSequece\",\"description\":\"\",\"root\":\"5ed70484-4723-41cf-80a3-1c3c29ee9648\",\"properties\":{},\"nodes\":{\"5ed70484-4723-41cf-80a3-1c3c29ee9648\":{\"id\":\"5ed70484-4723-41cf-80a3-1c3c29ee9648\",\"name\":\"Sequence\",\"category\":\"composite\",\"title\":\"Test: Sequence\",\"description\":\"\",\"properties\":{},\"display\":{\"x\":132,\"y\":-192},\"children\":[\"75450977-0f2d-4396-8318-fe5282d7053f\",\"f5c61b5a-70ea-419e-8a42-91297da94c3e\",\"0b221bd2-afd7-4970-834e-8adde66f8e69\"]},\"75450977-0f2d-4396-8318-fe5282d7053f\":{\"id\":\"75450977-0f2d-4396-8318-fe5282d7053f\",\"name\":\"Succeeder\",\"category\":\"action\",\"title\":\"Test: Sequence->Succeeder\",\"description\":\"\",\"properties\":{},\"display\":{\"x\":348,\"y\":-228}},\"2e42ea57-6315-4313-b059-62f86ae178a1\":{\"id\":\"2e42ea57-6315-4313-b059-62f86ae178a1\",\"name\":\"Failer\",\"category\":\"action\",\"title\":\"Test: Sequence->Failer\",\"description\":\"\",\"properties\":{},\"display\":{\"x\":768,\"y\":-144}},\"f5c61b5a-70ea-419e-8a42-91297da94c3e\":{\"id\":\"f5c61b5a-70ea-419e-8a42-91297da94c3e\",\"name\":\"Inverter\",\"category\":\"decorator\",\"title\":\"Test: Sequence->Inverter\",\"description\":\"\",\"properties\":{},\"display\":{\"x\":348,\"y\":-144},\"child\":\"2e42ea57-6315-4313-b059-62f86ae178a1\"},\"0b221bd2-afd7-4970-834e-8adde66f8e69\":{\"id\":\"0b221bd2-afd7-4970-834e-8adde66f8e69\",\"name\":\"Error\",\"category\":\"action\",\"title\":\"Test: Sequence->Error\",\"description\":\"\",\"properties\":{},\"display\":{\"x\":312,\"y\":-60}}},\"display\":{\"camera_x\":670.5,\"camera_y\":436,\"camera_z\":0.75,\"x\":-228,\"y\":-192}},{\"version\":\"0.3.0\",\"scope\":\"tree\",\"id\":\"73708c81-1698-4152-8cb9-9b78dfcc02e2\",\"title\":\"A behavior tree\",\"description\":\"\",\"root\":\"21504aad-c540-4336-9b9d-58002e82b871\",\"properties\":{},\"nodes\":{\"716905c2-bb11-4d39-80b0-aac66d780cb1\":{\"id\":\"716905c2-bb11-4d39-80b0-aac66d780cb1\",\"name\":\"Sequence\",\"category\":\"composite\",\"title\":\"Test: Sequence\",\"description\":\"\",\"properties\":{},\"display\":{\"x\":-132,\"y\":-384},\"children\":[\"ea75ca3a-14c9-47dd-84e5-852ef6af99a8\",\"dcb62256-afd8-474c-85b9-bf5f5d1185cd\"]},\"ea75ca3a-14c9-47dd-84e5-852ef6af99a8\":{\"id\":\"ea75ca3a-14c9-47dd-84e5-852ef6af99a8\",\"name\":\"Succeeder\",\"category\":\"action\",\"title\":\"Test: Sequence->Succeeder\",\"description\":\"\",\"properties\":{},\"display\":{\"x\":84,\"y\":-420}},\"04c385a8-90cb-4f8f-b9b0-77a543934a4b\":{\"id\":\"04c385a8-90cb-4f8f-b9b0-77a543934a4b\",\"name\":\"Failer\",\"category\":\"action\",\"title\":\"Test: Sequence->Failer\",\"description\":\"\",\"properties\":{},\"display\":{\"x\":288,\"y\":-336}},\"dcb62256-afd8-474c-85b9-bf5f5d1185cd\":{\"id\":\"dcb62256-afd8-474c-85b9-bf5f5d1185cd\",\"name\":\"Inverter\",\"category\":\"decorator\",\"title\":\"Test: Sequence->Inverter\",\"description\":\"\",\"properties\":{},\"display\":{\"x\":84,\"y\":-336},\"child\":\"04c385a8-90cb-4f8f-b9b0-77a543934a4b\"},\"1a20fc29-c17a-4b37-80d0-373c1a91baec\":{\"id\":\"1a20fc29-c17a-4b37-80d0-373c1a91baec\",\"name\":\"Priority\",\"category\":\"composite\",\"title\":\"Test: Priority\",\"description\":\"\",\"properties\":{},\"display\":{\"x\":-132,\"y\":-156},\"children\":[\"22008cb3-0bd7-45dd-8e5f-91421aac49a6\",\"ca22a957-361e-4bd9-9825-f19608459476\",\"f226e392-d6a6-42bf-bed4-bf59c2876634\"]},\"22008cb3-0bd7-45dd-8e5f-91421aac49a6\":{\"id\":\"22008cb3-0bd7-45dd-8e5f-91421aac49a6\",\"name\":\"Failer\",\"category\":\"action\",\"title\":\"Test: Priority->Failer\",\"description\":\"\",\"properties\":{},\"display\":{\"x\":84,\"y\":-252}},\"ca22a957-361e-4bd9-9825-f19608459476\":{\"id\":\"ca22a957-361e-4bd9-9825-f19608459476\",\"name\":\"Succeeder\",\"category\":\"action\",\"title\":\"Test: Priority->Succeeder\",\"description\":\"\",\"properties\":{},\"display\":{\"x\":84,\"y\":-156}},\"8eb0c633-4e77-42fd-85d7-e493f11b2208\":{\"id\":\"8eb0c633-4e77-42fd-85d7-e493f11b2208\",\"name\":\"Failer\",\"category\":\"action\",\"title\":\"Test: Priority->Failer\",\"description\":\"\",\"properties\":{},\"display\":{\"x\":288,\"y\":-72}},\"f226e392-d6a6-42bf-bed4-bf59c2876634\":{\"id\":\"f226e392-d6a6-42bf-bed4-bf59c2876634\",\"name\":\"Inverter\",\"category\":\"decorator\",\"title\":\"Test: Priority->Inverter\",\"description\":\"\",\"properties\":{},\"display\":{\"x\":84,\"y\":-72},\"child\":\"8eb0c633-4e77-42fd-85d7-e493f11b2208\"},\"21504aad-c540-4336-9b9d-58002e82b871\":{\"id\":\"21504aad-c540-4336-9b9d-58002e82b871\",\"name\":\"MemSequence\",\"category\":\"composite\",\"title\":\"Root\",\"description\":\"\",\"properties\":{},\"display\":{\"x\":-336,\"y\":12},\"children\":[\"716905c2-bb11-4d39-80b0-aac66d780cb1\",\"1a20fc29-c17a-4b37-80d0-373c1a91baec\",\"3801da2b-18f3-45fe-b6b0-3ab410d19095\",\"6338065f-4ac7-4413-8a8d-97540ff77583\",\"266105f9-9001-4a33-b5fb-0761ccd4e3fd\"]},\"3801da2b-18f3-45fe-b6b0-3ab410d19095\":{\"id\":\"3801da2b-18f3-45fe-b6b0-3ab410d19095\",\"name\":\"MemSequence\",\"category\":\"composite\",\"title\":\"Test: MemSequence\",\"description\":\"\",\"properties\":{},\"display\":{\"x\":-132,\"y\":144},\"children\":[\"30380ca4-57f7-43f0-8299-474aa8c38de9\",\"18532990-0aaa-4b63-8b24-75390713cddb\",\"b27af6a0-a8e7-460f-8cbc-e6b6312c3f70\",\"6f112a51-db75-4468-824f-ac2519ec6511\"]},\"30380ca4-57f7-43f0-8299-474aa8c38de9\":{\"id\":\"30380ca4-57f7-43f0-8299-474aa8c38de9\",\"name\":\"Repeater\",\"category\":\"decorator\",\"title\":\"Test: MemSequence->Repeat <maxLoop>x\",\"description\":\"\",\"properties\":{\"maxLoop\":2},\"display\":{\"x\":108,\"y\":12},\"child\":\"d6c80541-7ec3-4308-99e7-026f35518db6\"},\"18532990-0aaa-4b63-8b24-75390713cddb\":{\"id\":\"18532990-0aaa-4b63-8b24-75390713cddb\",\"name\":\"RepeatUntilSuccess\",\"category\":\"decorator\",\"title\":\"Repeat Until Success\",\"description\":\"\",\"properties\":{\"maxLoop\":-1},\"display\":{\"x\":84,\"y\":108},\"child\":\"0656aec2-3d23-47ff-825b-2311664b67a5\"},\"04e70d44-e4aa-4c27-98b5-7edadfb40882\":{\"id\":\"04e70d44-e4aa-4c27-98b5-7edadfb40882\",\"name\":\"Succeeder\",\"category\":\"action\",\"title\":\"Succeeder\",\"description\":\"\",\"properties\":{},\"display\":{\"x\":708,\"y\":108}},\"6617e972-62d8-4db9-83b0-428eb938f6fa\":{\"id\":\"6617e972-62d8-4db9-83b0-428eb938f6fa\",\"name\":\"RepeatUntilFailure\",\"category\":\"decorator\",\"title\":\"Repeat Until Failure\",\"description\":\"\",\"properties\":{\"maxLoop\":-1},\"display\":{\"x\":288,\"y\":192},\"child\":\"203f8898-8e80-4886-851c-8f72857512d0\"},\"bef8b7ca-d0b2-4ab5-8583-fd9507171190\":{\"id\":\"bef8b7ca-d0b2-4ab5-8583-fd9507171190\",\"name\":\"Limiter\",\"category\":\"decorator\",\"title\":\"Limit <maxLoop> Activations\",\"description\":\"\",\"properties\":{\"maxLoop\":2},\"display\":{\"x\":492,\"y\":108},\"child\":\"04e70d44-e4aa-4c27-98b5-7edadfb40882\"},\"0656aec2-3d23-47ff-825b-2311664b67a5\":{\"id\":\"0656aec2-3d23-47ff-825b-2311664b67a5\",\"name\":\"Inverter\",\"category\":\"decorator\",\"title\":\"Inverter\",\"description\":\"\",\"properties\":{},\"display\":{\"x\":288,\"y\":108},\"child\":\"bef8b7ca-d0b2-4ab5-8583-fd9507171190\"},\"b27af6a0-a8e7-460f-8cbc-e6b6312c3f70\":{\"id\":\"b27af6a0-a8e7-460f-8cbc-e6b6312c3f70\",\"name\":\"Inverter\",\"category\":\"decorator\",\"title\":\"Inverter\",\"description\":\"\",\"properties\":{},\"display\":{\"x\":84,\"y\":192},\"child\":\"6617e972-62d8-4db9-83b0-428eb938f6fa\"},\"203f8898-8e80-4886-851c-8f72857512d0\":{\"id\":\"203f8898-8e80-4886-851c-8f72857512d0\",\"name\":\"Limiter\",\"category\":\"decorator\",\"title\":\"Limit <maxLoop> Activations\",\"description\":\"\",\"properties\":{\"maxLoop\":1},\"display\":{\"x\":492,\"y\":192},\"child\":\"62ad352b-eb1d-4492-8757-0e3934344cb4\"},\"62ad352b-eb1d-4492-8757-0e3934344cb4\":{\"id\":\"62ad352b-eb1d-4492-8757-0e3934344cb4\",\"name\":\"Succeeder\",\"category\":\"action\",\"title\":\"Succeeder\",\"description\":\"\",\"properties\":{},\"display\":{\"x\":708,\"y\":192}},\"d6c80541-7ec3-4308-99e7-026f35518db6\":{\"id\":\"d6c80541-7ec3-4308-99e7-026f35518db6\",\"name\":\"Succeeder\",\"category\":\"action\",\"title\":\"Test: Repeat->Succeeder\",\"description\":\"\",\"properties\":{},\"display\":{\"x\":504,\"y\":12}},\"5bb2771b-9cc9-4331-8c92-6e66fe0c8920\":{\"id\":\"5bb2771b-9cc9-4331-8c92-6e66fe0c8920\",\"name\":\"Wait\",\"category\":\"action\",\"title\":\"Wait <milliseconds>ms\",\"description\":\"\",\"properties\":{\"milliseconds\":3000},\"display\":{\"x\":288,\"y\":276}},\"6f112a51-db75-4468-824f-ac2519ec6511\":{\"id\":\"6f112a51-db75-4468-824f-ac2519ec6511\",\"name\":\"MemSequence\",\"category\":\"composite\",\"title\":\"MemSequence\",\"description\":\"\",\"properties\":{},\"display\":{\"x\":84,\"y\":276},\"children\":[\"5bb2771b-9cc9-4331-8c92-6e66fe0c8920\"]},\"2dae4084-f494-4e6b-8b62-a725df7080fa\":{\"id\":\"2dae4084-f494-4e6b-8b62-a725df7080fa\",\"name\":\"MaxTime\",\"category\":\"decorator\",\"title\":\"Max <maxTime>ms\",\"description\":\"\",\"properties\":{\"maxTime\":4000},\"display\":{\"x\":288,\"y\":372}},\"6338065f-4ac7-4413-8a8d-97540ff77583\":{\"id\":\"6338065f-4ac7-4413-8a8d-97540ff77583\",\"name\":\"MemPriority\",\"category\":\"composite\",\"title\":\"MemPriority\",\"description\":\"\",\"properties\":{},\"display\":{\"x\":-132,\"y\":408},\"children\":[\"27eca90b-ee8f-48d6-83f7-f64fede7cf97\"]},\"a3ec891f-4248-4aad-8d14-90df93e32cc8\":{\"id\":\"a3ec891f-4248-4aad-8d14-90df93e32cc8\",\"name\":\"Runner\",\"category\":\"action\",\"title\":\"Runner\",\"description\":\"\",\"properties\":{},\"display\":{\"x\":288,\"y\":456}},\"27eca90b-ee8f-48d6-83f7-f64fede7cf97\":{\"id\":\"27eca90b-ee8f-48d6-83f7-f64fede7cf97\",\"name\":\"Sequence\",\"category\":\"composite\",\"title\":\"Sequence\",\"description\":\"\",\"properties\":{},\"display\":{\"x\":84,\"y\":408},\"children\":[\"2dae4084-f494-4e6b-8b62-a725df7080fa\",\"a3ec891f-4248-4aad-8d14-90df93e32cc8\"]},\"266105f9-9001-4a33-b5fb-0761ccd4e3fd\":{\"id\":\"266105f9-9001-4a33-b5fb-0761ccd4e3fd\",\"name\":\"Error\",\"category\":\"action\",\"title\":\"Error\",\"description\":\"\",\"properties\":{},\"display\":{\"x\":-84,\"y\":540}}},\"display\":{\"camera_x\":849,\"camera_y\":410.5,\"camera_z\":1,\"x\":-540,\"y\":-12}}],\"custom_nodes\":[]}") {
            Ok(_c) => { _c }
            Err(_e) => {
                assert_eq!("", _e.to_string());
                return
            }
        };
        let mut b3 = match B3::new_default(config) {
            Ok(_b) => { _b }
            Err(_e) => {
                assert_eq!("", _e);
                return;
            }
        };
        while B3::tick(&mut b3) != NodeStatus::ERROR {
            std::thread::sleep(Duration::from_secs(1));
        }
    }

    /// test output: sequence->priority->fauker->succeeder->error
    #[test]
    fn test_priority() {
        let config: serde_json::Value = match serde_json::from_str("{\"version\":\"0.3.0\",\"scope\":\"tree\",\"id\":\"9280c068-3754-440a-8048-9693651901eb\",\"title\":\"TestPriority\",\"description\":\"\",\"root\":\"e32f539b-75d5-444f-80fb-da530f56a993\",\"properties\":{},\"nodes\":{\"0a960049-bbc8-4cbc-bec4-4fe090684989\":{\"id\":\"0a960049-bbc8-4cbc-bec4-4fe090684989\",\"name\":\"Priority\",\"category\":\"composite\",\"title\":\"Test: Priority\",\"description\":\"\",\"properties\":{},\"display\":{\"x\":-120,\"y\":-108},\"children\":[\"b08b3b8a-51e0-425b-8100-75d1b92b9333\",\"8adcbcff-cb08-49f8-87e4-dad30fada9df\",\"6b6b68eb-f55a-47fc-8398-85d76b9b1d78\"]},\"b08b3b8a-51e0-425b-8100-75d1b92b9333\":{\"id\":\"b08b3b8a-51e0-425b-8100-75d1b92b9333\",\"name\":\"Failer\",\"category\":\"action\",\"title\":\"Test: Priority->Failer\",\"description\":\"\",\"properties\":{},\"display\":{\"x\":120,\"y\":-192}},\"8adcbcff-cb08-49f8-87e4-dad30fada9df\":{\"id\":\"8adcbcff-cb08-49f8-87e4-dad30fada9df\",\"name\":\"Succeeder\",\"category\":\"action\",\"title\":\"Test: Priority->Succeeder\",\"description\":\"\",\"properties\":{},\"display\":{\"x\":168,\"y\":-108}},\"d27de3bb-e415-442b-804d-18901fc86e9b\":{\"id\":\"d27de3bb-e415-442b-804d-18901fc86e9b\",\"name\":\"Failer\",\"category\":\"action\",\"title\":\"Test: Priority->Failer\",\"description\":\"\",\"properties\":{},\"display\":{\"x\":420,\"y\":-24}},\"6b6b68eb-f55a-47fc-8398-85d76b9b1d78\":{\"id\":\"6b6b68eb-f55a-47fc-8398-85d76b9b1d78\",\"name\":\"Inverter\",\"category\":\"decorator\",\"title\":\"Test: Priority->Inverter\",\"description\":\"\",\"properties\":{},\"display\":{\"x\":168,\"y\":-24},\"child\":\"d27de3bb-e415-442b-804d-18901fc86e9b\"},\"e32f539b-75d5-444f-80fb-da530f56a993\":{\"id\":\"e32f539b-75d5-444f-80fb-da530f56a993\",\"name\":\"Sequence\",\"category\":\"composite\",\"title\":\"Sequence\",\"description\":\"\",\"properties\":{},\"display\":{\"x\":-228,\"y\":-108},\"children\":[\"0a960049-bbc8-4cbc-bec4-4fe090684989\",\"789da59f-3bbc-48b6-8278-855004e4e1b9\"]},\"789da59f-3bbc-48b6-8278-855004e4e1b9\":{\"id\":\"789da59f-3bbc-48b6-8278-855004e4e1b9\",\"name\":\"Error\",\"category\":\"action\",\"title\":\"Test: Priority->Error\",\"description\":\"\",\"properties\":{},\"display\":{\"x\":-48,\"y\":24}}},\"display\":{\"camera_x\":680,\"camera_y\":374.5,\"camera_z\":1,\"x\":-336,\"y\":-108},\"custom_nodes\":[]}") {
            Ok(_c) => { _c }
            Err(_e) => {
                assert_eq!("", _e.to_string());
                return
            }
        };
        let mut b3 = match B3::new_default(config) {
            Ok(_b) => { _b }
            Err(_e) => {
                assert_eq!("", _e);
                return;
            }
        };
        while B3::tick(&mut b3) != NodeStatus::ERROR {
            std::thread::sleep(Duration::from_secs(1));
        }
    }

    /// test output: mem_sequence->repeat->succeeder->repeat->succeeder->repeat until success -> inverter -> limit -> succeeder->invert->repeat until failure->limit->succeeder->mem_sequence->wait 3000->error
    #[test]
    fn test_mem_sequence() {
        let config: serde_json::Value = match serde_json::from_str("{\"version\":\"0.3.0\",\"scope\":\"tree\",\"id\":\"3b9a62f1-43be-4fce-86ee-b2e38d669397\",\"title\":\"TestMemSequence\",\"description\":\"\",\"root\":\"dab92d03-faf6-4c9b-8214-40f66ce21945\",\"properties\":{},\"nodes\":{\"dab92d03-faf6-4c9b-8214-40f66ce21945\":{\"id\":\"dab92d03-faf6-4c9b-8214-40f66ce21945\",\"name\":\"MemSequence\",\"category\":\"composite\",\"title\":\"Test: MemSequence\",\"description\":\"\",\"properties\":{},\"display\":{\"x\":-336,\"y\":-84},\"children\":[\"527ee20f-fac4-4e42-a19f-67bc7bf5fdae\",\"ef0d5d5c-b93d-4ddd-8c7b-c86a5d446a1f\",\"a1a18cf1-b079-4108-b55c-c3ac552e4ffd\",\"63f3e07a-99b0-4c5e-ab7e-9f2425ee2294\",\"17855308-14d4-4990-84fa-c2fc5f5ce6fa\"]},\"527ee20f-fac4-4e42-a19f-67bc7bf5fdae\":{\"id\":\"527ee20f-fac4-4e42-a19f-67bc7bf5fdae\",\"name\":\"Repeater\",\"category\":\"decorator\",\"title\":\"Test: MemSequence->Repeat <maxLoop>x\",\"description\":\"\",\"properties\":{\"maxLoop\":2},\"display\":{\"x\":-12,\"y\":-288},\"child\":\"4bef1dad-3492-4857-8351-ba68c2b7eaf8\"},\"ef0d5d5c-b93d-4ddd-8c7b-c86a5d446a1f\":{\"id\":\"ef0d5d5c-b93d-4ddd-8c7b-c86a5d446a1f\",\"name\":\"RepeatUntilSuccess\",\"category\":\"decorator\",\"title\":\"Repeat Until Success\",\"description\":\"\",\"properties\":{\"maxLoop\":-1},\"display\":{\"x\":-96,\"y\":-156},\"child\":\"29a365f5-578f-4336-88cc-144f513d04b2\"},\"a80886f0-3ce2-4777-8f6e-f8c0b671dba4\":{\"id\":\"a80886f0-3ce2-4777-8f6e-f8c0b671dba4\",\"name\":\"RepeatUntilFailure\",\"category\":\"decorator\",\"title\":\"Repeat Until Failure\",\"description\":\"\",\"properties\":{\"maxLoop\":-1},\"display\":{\"x\":84,\"y\":48},\"child\":\"4aa45f42-8388-4578-83a6-d498f6df6057\"},\"554786ff-455b-47bb-b792-1d58f116d9ad\":{\"id\":\"554786ff-455b-47bb-b792-1d58f116d9ad\",\"name\":\"Limiter\",\"category\":\"decorator\",\"title\":\"Limit <maxLoop> Activations\",\"description\":\"\",\"properties\":{\"maxLoop\":2},\"display\":{\"x\":336,\"y\":-156},\"child\":\"ab576ee0-3229-4ece-8df9-344a2a52f264\"},\"29a365f5-578f-4336-88cc-144f513d04b2\":{\"id\":\"29a365f5-578f-4336-88cc-144f513d04b2\",\"name\":\"Inverter\",\"category\":\"decorator\",\"title\":\"Inverter\",\"description\":\"\",\"properties\":{},\"display\":{\"x\":120,\"y\":-156},\"child\":\"554786ff-455b-47bb-b792-1d58f116d9ad\"},\"a1a18cf1-b079-4108-b55c-c3ac552e4ffd\":{\"id\":\"a1a18cf1-b079-4108-b55c-c3ac552e4ffd\",\"name\":\"Inverter\",\"category\":\"decorator\",\"title\":\"Inverter\",\"description\":\"\",\"properties\":{},\"display\":{\"x\":-120,\"y\":48},\"child\":\"a80886f0-3ce2-4777-8f6e-f8c0b671dba4\"},\"4aa45f42-8388-4578-83a6-d498f6df6057\":{\"id\":\"4aa45f42-8388-4578-83a6-d498f6df6057\",\"name\":\"Limiter\",\"category\":\"decorator\",\"title\":\"Limit <maxLoop> Activations\",\"description\":\"\",\"properties\":{\"maxLoop\":1},\"display\":{\"x\":360,\"y\":48},\"child\":\"263f5a46-851e-48d6-a1aa-45ca583a72f6\"},\"263f5a46-851e-48d6-a1aa-45ca583a72f6\":{\"id\":\"263f5a46-851e-48d6-a1aa-45ca583a72f6\",\"name\":\"Succeeder\",\"category\":\"action\",\"title\":\"Succeeder\",\"description\":\"\",\"properties\":{},\"display\":{\"x\":612,\"y\":48}},\"4bef1dad-3492-4857-8351-ba68c2b7eaf8\":{\"id\":\"4bef1dad-3492-4857-8351-ba68c2b7eaf8\",\"name\":\"Succeeder\",\"category\":\"action\",\"title\":\"Test: Repeat->Succeeder\",\"description\":\"\",\"properties\":{},\"display\":{\"x\":336,\"y\":-288}},\"5749d8ea-70ff-42f1-861f-09a23c269dff\":{\"id\":\"5749d8ea-70ff-42f1-861f-09a23c269dff\",\"name\":\"Wait\",\"category\":\"action\",\"title\":\"Wait <milliseconds>ms\",\"description\":\"\",\"properties\":{\"milliseconds\":3000},\"display\":{\"x\":72,\"y\":252}},\"63f3e07a-99b0-4c5e-ab7e-9f2425ee2294\":{\"id\":\"63f3e07a-99b0-4c5e-ab7e-9f2425ee2294\",\"name\":\"MemSequence\",\"category\":\"composite\",\"title\":\"MemSequence\",\"description\":\"\",\"properties\":{},\"display\":{\"x\":-132,\"y\":252},\"children\":[\"5749d8ea-70ff-42f1-861f-09a23c269dff\"]},\"17855308-14d4-4990-84fa-c2fc5f5ce6fa\":{\"id\":\"17855308-14d4-4990-84fa-c2fc5f5ce6fa\",\"name\":\"Error\",\"category\":\"action\",\"title\":\"Error\",\"description\":\"\",\"properties\":{},\"display\":{\"x\":-84,\"y\":336}},\"ab576ee0-3229-4ece-8df9-344a2a52f264\":{\"id\":\"ab576ee0-3229-4ece-8df9-344a2a52f264\",\"name\":\"Succeeder\",\"category\":\"action\",\"title\":\"Succeeder\",\"description\":\"\",\"properties\":{},\"display\":{\"x\":564,\"y\":-156}}},\"display\":{\"camera_x\":680,\"camera_y\":374.5,\"camera_z\":0.75,\"x\":-432,\"y\":-120},\"custom_nodes\":[]}") {
            Ok(_c) => { _c }
            Err(_e) => {
                assert_eq!("", _e.to_string());
                return
            }
        };
        let mut b3 = match B3::new_default(config) {
            Ok(_b) => { _b }
            Err(_e) => {
                assert_eq!("", _e);
                return;
            }
        };
        while B3::tick(&mut b3) != NodeStatus::ERROR {
            std::thread::sleep(Duration::from_secs(1));
        }
    }

    /// test output: sequence->MemPriority->Max(4secs)->Runner->Failer->Succeeder->error
    #[test]
    fn test_mem_priority() {
        let config: serde_json::Value = match serde_json::from_str("{\"version\":\"0.3.0\",\"scope\":\"tree\",\"id\":\"000b0d81-c90c-4d04-b463-ff2bc3a223e9\",\"title\":\"TestMemPriority\",\"description\":\"\",\"root\":\"5ddfaece-4e71-4d3c-8c94-f26a71998936\",\"properties\":{},\"nodes\":{\"7efb22cb-d203-43d1-9646-5d9ec24bce93\":{\"id\":\"7efb22cb-d203-43d1-9646-5d9ec24bce93\",\"name\":\"MaxTime\",\"category\":\"decorator\",\"title\":\"Max <maxTime>ms\",\"description\":\"\",\"properties\":{\"maxTime\":4000},\"display\":{\"x\":36,\"y\":-120},\"child\":\"f4d87220-22e7-4b60-97f9-6cdb4ae19cf8\"},\"b9b5f260-33d1-41c8-8c53-efea68de0d60\":{\"id\":\"b9b5f260-33d1-41c8-8c53-efea68de0d60\",\"name\":\"MemPriority\",\"category\":\"composite\",\"title\":\"MemPriority\",\"description\":\"\",\"properties\":{},\"display\":{\"x\":-264,\"y\":-120},\"children\":[\"7efb22cb-d203-43d1-9646-5d9ec24bce93\",\"58818870-17c0-4a8b-b9b8-22c7d76106ba\",\"97fc2d58-c716-4c75-8a66-baa438ddfe83\",\"719d091c-8a65-45a4-8c1a-601413ea1cda\"]},\"58818870-17c0-4a8b-b9b8-22c7d76106ba\":{\"id\":\"58818870-17c0-4a8b-b9b8-22c7d76106ba\",\"name\":\"Failer\",\"category\":\"action\",\"title\":\"Failer\",\"description\":\"\",\"properties\":{},\"display\":{\"x\":36,\"y\":-48}},\"953d95bb-9c0e-4d4b-9ad6-995ac3b40193\":{\"id\":\"953d95bb-9c0e-4d4b-9ad6-995ac3b40193\",\"name\":\"Error\",\"category\":\"action\",\"title\":\"Error\",\"description\":\"\",\"properties\":{},\"display\":{\"x\":12,\"y\":156}},\"97fc2d58-c716-4c75-8a66-baa438ddfe83\":{\"id\":\"97fc2d58-c716-4c75-8a66-baa438ddfe83\",\"name\":\"Succeeder\",\"category\":\"action\",\"title\":\"Succeeder\",\"description\":\"\",\"properties\":{},\"display\":{\"x\":36,\"y\":12}},\"f4d87220-22e7-4b60-97f9-6cdb4ae19cf8\":{\"id\":\"f4d87220-22e7-4b60-97f9-6cdb4ae19cf8\",\"name\":\"Runner\",\"category\":\"action\",\"title\":\"Runner\",\"description\":\"\",\"properties\":{},\"display\":{\"x\":252,\"y\":-120}},\"719d091c-8a65-45a4-8c1a-601413ea1cda\":{\"id\":\"719d091c-8a65-45a4-8c1a-601413ea1cda\",\"name\":\"Failer\",\"category\":\"action\",\"title\":\"Failer\",\"description\":\"\",\"properties\":{},\"display\":{\"x\":36,\"y\":72}},\"5ddfaece-4e71-4d3c-8c94-f26a71998936\":{\"id\":\"5ddfaece-4e71-4d3c-8c94-f26a71998936\",\"name\":\"Sequence\",\"category\":\"composite\",\"title\":\"Sequence\",\"description\":\"\",\"properties\":{},\"display\":{\"x\":-432,\"y\":-120},\"children\":[\"b9b5f260-33d1-41c8-8c53-efea68de0d60\",\"953d95bb-9c0e-4d4b-9ad6-995ac3b40193\"]}},\"display\":{\"camera_x\":816,\"camera_y\":387.5,\"camera_z\":1,\"x\":-516,\"y\":-120},\"custom_nodes\":[]}") {
            Ok(_c) => { _c }
            Err(_e) => {
                assert_eq!("", _e.to_string());
                return
            }
        };
        let mut b3 = match B3::new_default(config) {
            Ok(_b) => { _b }
            Err(_e) => {
                assert_eq!("", _e);
                return;
            }
        };
        while B3::tick(&mut b3) != NodeStatus::ERROR {
            std::thread::sleep(Duration::from_secs(1));
        }
    }

}